<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 论坛管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
                <el-button type="primary" icon="el-icon-search" @click="getData">刷新</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
            >
                <el-table-column prop="title" label="标题" width="180"></el-table-column>、
                <el-table-column prop="content" label="内容"></el-table-column>
                <el-table-column prop="createdTime" label="创建时间" width="180"></el-table-column>
                <el-table-column label="操作" width="210" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-zoom-in"
                            class="origin"
                            @click="handleShow(scope.$index, scope.row)"
                        >查看</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑帖子" :visible.sync="editVisible" width="40%" :close-on-click-modal="false">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="标题">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input type="textarea" v-model="form.content"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 查看帖子 -->
        <el-dialog title="查看帖子" :visible.sync="showVisible" width="900px" :close-on-click-modal="false">
            <el-table
               :data="showTable"
               stripe
               style="width: 100%">
               <el-table-column
                 prop="content"
                 label="内容">
               </el-table-column>
               <el-table-column
                 prop="creator"
                 label="用户"
                 width="180">
               </el-table-column>
               <el-table-column
                 prop="createdTime"
                 width="180"
                 label="回复时间">
               </el-table-column>
               <el-table-column label="操作" width="80" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red mid"
                            @click="childDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
           </el-table>               
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                title: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            editVisible: false,
            showVisible:false,
            pageTotal: 0,
            form: {},
            showTable:[],

        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.$http.postHead('hgz/post/queryListWithPage',this.query).then(res=>{
                console.log(res);
                this.tableData = res.data.list;
            })
        },
        // 触发添加按钮
        handleAdd() {
            this.form = {};
            this.editVisible = true;
        },
        childDelete(index, row) {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                    // hgz/post/deleteById
                this.$http.postHead(`hgz/replay/deleteById/${row.id}`)
                .then(res =>{
                    if(res.status){
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getData();
                    }else{
                        this.$message({
                            type: 'error',
                            message: res.message
                        });   
                    }
                })

            }).catch(() => {

            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                    // hgz/post/deleteById
                this.$http.postHead(`hgz/post/deleteById/${row.id}`)
                .then(res =>{
                    if(res.status){
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }else{
                        this.$message({
                            type: 'error',
                            message: res.message
                        });   
                    }
                })

            }).catch(() => {

            });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.form = {
                id:row.id,
                title:row.title,
                content:row.content
            };
            this.editVisible = true;
        },
        handleShow(index,row){
            // hgz/post/showById/{id}
            this.$http.postHead(`hgz/post/showById/${row.id}`).then(res =>{
                if(res.status){
                    this.showVisible = true;
                    this.showTable = res.data.replayVoList;
                }
            })
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            // hgz/post/save
            this.$http.postJson('hgz/post/save',this.form).then(res=>{
                console.log(res);
                if(res.status){
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                        this.editVisible = false;
                        this.getData();
                }else{
                        this.$message({
                            type: 'error',
                            message: res.message
                        });  
                }
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.origin {
    color: #ff7300;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.boxshadow{
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    margin: 5px 0px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
